package com.iss.card.view.mycard.detail;

import java.util.ArrayList;

import com.iss.card.R;
import com.iss.card.common.ApplicationManager;
import com.iss.card.common.IBackKeyListener;
import com.iss.card.view.model.CardDetailBrowsetModel;
import com.iss.card.view.model.CardHistoryBrowseModel;
import com.iss.card.view.model.ResponseBean;
import com.iss.card.view.mycard.CardBagModel;
import com.iss.card.view.mycard.MyCardDetailActivity;
import com.iss.card.view.mycard.detail.CardDetailHistroyFragment.HistoryListAdapter;
import com.iss.card.webservice.WebServiceAsyncService;
import com.iss.card.webservice.WebServiceAsyncServiceHelper;
import com.iss.card.webservice.WebServiceUtils;
import com.iss.card.webservice.WebServiceAsyncService.Operation;
import com.iss.card.webservice.WebServiceAsyncService.PostExecute;
import com.iss.card.webservice.WebServiceManager.WebServiceRegistrationParameter;
import com.iss.card.webservice.WebServiceManager.WebServiceUrlPortManager;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CardDetailStoreBrowseFragment extends Fragment implements IBackKeyListener{

	
	private FragmentActivity mContext;
	private View mainView;
	private ListView itemContainer;
	private CardBagModel  cardBagModel;
	
	
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mContext = (FragmentActivity)activity;
		cardBagModel = (CardBagModel) getArguments().getSerializable(ApplicationManager.KEY_MYCARD_BRIEF_CARD);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mainView = new View(mContext).inflate(mContext, R.layout.card_store_browse, null);
		itemContainer = (ListView) mainView.findViewById(R.id.store_browse_item_container);
		getStoreListInfo();
		return mainView;
	}
	
	private void getStoreListInfo(){
		
		String[] parameterValues = {cardBagModel.getCardId()};

		WebServiceAsyncService asyncService = new WebServiceAsyncService();
		asyncService.setParameterToAsyncService(
				
				mContext, 
				asyncService.getNextToken(), 
				Operation.OP_SERVICE_ARG_STORE_CARD_ADDRESS, 
				WebServiceRegistrationParameter.PRODUCT_STORE_CARD_ADDRESS, 
				parameterValues, 
				WebServiceUrlPortManager.ACTION_URL_PORT_STORE_CARD_ADDRESS,
				this,
				WebServiceAsyncServiceHelper.TYPE_HTTP_GET_INSTREAM
				
				);
		asyncService.setPostExecute(new PostExecute() {
			@Override
			public void onAsyncServiceComplete(int token, Object envelope,int index) {
				ResponseBean response = WebServiceUtils.getObjectResultToBaseInfo(envelope, CardHistoryBrowseModel.class);
				ArrayList<CardHistoryBrowseModel> historyTraceInfos = 
						(ArrayList<CardHistoryBrowseModel>) response.getReturnList();
			
				itemContainer.setAdapter(new StoreBrowseAdapter(historyTraceInfos, mContext));
			}
		});
	}

	@Override
	public void cancelUpdate(Object... params) {
		// TODO Auto-generated method stub
		
	}
	
	class StoreBrowseAdapter extends BaseAdapter{

		private ArrayList<CardHistoryBrowseModel> mItemList;
		private Context mContext;
		private LayoutInflater inflater;
		public StoreBrowseAdapter(ArrayList<CardHistoryBrowseModel> list,FragmentActivity context ){
			this.mItemList = list;
			this.mContext = context;
			inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mItemList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			StoreBrowseViewHolder holder = new StoreBrowseViewHolder();
			CardHistoryBrowseModel model = mItemList.get(position);
			if(convertView == null){
				convertView = inflater.inflate(R.layout.card_store_browse_item, null);
				holder.name = (TextView) convertView.findViewById(R.id.store_browse_store_name);
				holder.adress = (TextView) convertView.findViewById(R.id.store_browse_store_adress);
				convertView.setTag(holder);
			}else {
				holder = (StoreBrowseViewHolder)convertView.getTag();
			}
			holder.adress.setText(model.getStoreAddress());
			return convertView;
		}
		
		
	}
	static class StoreBrowseViewHolder{
		
		TextView name,adress;
	}
}
